package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.PledgeTaskInfoDao;
import com.sc.nft.dao.PledgeTaskVigorousCollectionDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.PledgeTaskCollectionVO;
import com.sc.nft.entity.vo.UserHoldCollectionVO;
import com.sc.nft.enums.PledgeStatusEnum;
import com.sc.nft.enums.VigorousStatusEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.*;
import com.sc.nft.sup.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 质押任务-激活NFT服务接口实现
 *
 * @author Sun
 * @since 2023-06-12 15:36:56
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PledgeTaskVigorousCollectionServiceImpl implements PledgeTaskVigorousCollectionService {
    private final PledgeTaskVigorousCollectionDao pledgeTaskVigorousCollectionDao;
    private final UserVigorousCollectionRecordsService userVigorousCollectionRecordsService;
    private final PledgeTaskInfoDao pledgeTaskInfoDao;
    private final UserCollectionService userCollectionService;
    private final PledgeTaskCollectionService pledgeTaskCollectionService;
    private final UserPledgeCollectionRecordsService userPledgeCollectionRecordsService;

    @Override
    public List<PledgeTaskVigorousCollection> getListByPledgeTaskId(Long pledgeTaskId) {
        return pledgeTaskVigorousCollectionDao.getListByPledgeTaskId(pledgeTaskId);
    }

    @Override
    public Page<UserHoldCollectionVO> secretAreaVigorousDetails(Long pledgeTaskId,Long id,Integer type,  Long collectionId, UserInfo user, Integer pageNo, Integer pageSize) {
        PledgeTaskInfo pledgeTaskInfo = pledgeTaskInfoDao.getById(pledgeTaskId);
        Assert.notNull(pledgeTaskInfo,()->new GlobalRunTimeException("秘境不存在"));

        if(type ==1){//激活任务
            //校验用户是否已经激活
            Integer count = userVigorousCollectionRecordsService.getOneByUserIdAndPledgeTaskIdAndVigorousStatus(user.getId(), pledgeTaskId, VigorousStatusEnum.ACTIVATED);
            Assert.isTrue(count <= 0 ,()->new GlobalRunTimeException("请勿重复激活"));

            //校验激活藏品是否与传入的藏品一致
            List<PledgeTaskVigorousCollection> vigorousCollectionList = pledgeTaskVigorousCollectionDao.getListByPledgeTaskId(pledgeTaskId);
            List<Long> collect = vigorousCollectionList.stream().map(PledgeTaskVigorousCollection::getCollectionId).collect(Collectors.toList());
            Assert.isTrue(collect.contains(collectionId),()->new GlobalRunTimeException("未持有需要激活的藏品"));
        }else if (type ==2){//质押任务
            Integer vigorousCount = userVigorousCollectionRecordsService.getOneByUserIdAndPledgeTaskIdAndVigorousStatus(user.getId(), pledgeTaskId, VigorousStatusEnum.ACTIVATED);
            Assert.isTrue(vigorousCount >= 1 ,()->new GlobalRunTimeException("请先激活秘境"));

            List<PledgeTaskCollection> list = pledgeTaskCollectionService.getListByPledgeTaskId(pledgeTaskId);
            Assert.isTrue(list.size() >=1 ,()->new GlobalRunTimeException("质押任务不存在"));

            if(!pledgeTaskInfo.getIsRepeatedly()){//不可以重复质押
//                Integer pledgeCollectionCount = userPledgeCollectionRecordsService.getCountByUserIdAndPledgeTaskIdAndStatus(user.getId(), pledgeTaskInfo.getId(), PledgeStatusEnum.IN_PLEDGE);
                Integer pledgeCollectionCount = userPledgeCollectionRecordsService.getCountByUserIdAndPledgeTaskIdAndPledgeTaskCollectionIdAndStatus(user.getId(), pledgeTaskInfo.getId(),id, PledgeStatusEnum.IN_PLEDGE);
                if(pledgeCollectionCount >= 1){
                    throw new GlobalRunTimeException("不可以重复质押");
                }
            }
        }

        return userCollectionService.getPageByUserIdAndCollectionId(user.getId(),collectionId,pageNo,pageSize);
    }

    @Override
    public PledgeTaskVigorousCollection getById(Long id) {
        return pledgeTaskVigorousCollectionDao.getById(id);
    }

    @Override
    public PledgeTaskVigorousCollection getByIdAndPledgeTaskId(Long id, Long pledgeTaskId) {
        return pledgeTaskVigorousCollectionDao.getByIdAndPledgeTaskId(id,pledgeTaskId);
    }
}